package cn.b.sky.mes.mm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.mm.model.Supplier;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.mm.model.condition.SupplierCondition;
import cn.b.sky.mes.mm.service.SupplierService;
import cn.b.sky.common.exception.ImeException;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

/**
 * 
 * <strong>Title : SupplierController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.mm.controller<br>
 * </strong> <strong>Description : </strong>供应商控制器<br>
 * <strong>Create on : 2016-2-29 下午4:15:34<br>
 * </strong>
 * <p>
 *  
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 *          <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 *          <br>
 *          <br>
 */
@Controller
@RequestMapping("/supplier/*")
public class SupplierController extends SecurityController {

  @Resource
  public SupplierService supplierService;

  @RequestMapping("supplierList")
  public String supplierList(@ModelAttribute("condition") SupplierCondition condition, String navTabId, Model model) {
    condition.setSiteCode(this.getSiteCode());
    List<Supplier> suppliers = supplierService.getSuppliersByCondition(condition);
    int count = supplierService.getSupplierCountByCondition(condition);
    condition.setData(suppliers);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute("navTabId", navTabId);
    return "mm/supplier/supplier_list";
  }

  @RequestMapping("addSupplier")
  public String addSupplier(String navTabId, Model model) {
    model.addAttribute("navTabId", navTabId);
    model.addAttribute("type", "add");
    return "mm/supplier/supplier_add";
  }

  @RequestMapping("modifySupplier")
  public String modifySupplier(String navTabId, String supplierCode, Model model) {
    if (StringUtils.isBlank(supplierCode)) {
      throw ImeException.newException("parameterNull.M");
    }
    Supplier supplier = supplierService.getSupplierBySupplierCode(this.getSiteCode(), supplierCode);
    model.addAttribute("supplier", supplier);
    model.addAttribute("navTabId", navTabId);
    model.addAttribute("type", "modify");
    return "mm/supplier/supplier_add";
  }

  @RequestMapping("viewSupplier")
  public String viewSupplier(String supplierCode, String navTabId, Model model) {
    if (StringUtils.isBlank(supplierCode)) {
      throw ImeException.newException("parameterNull.M");
    }
    Supplier supplier = supplierService.getSupplierBySupplierCode(this.getSiteCode(), supplierCode);
    model.addAttribute("supplier", supplier);
    model.addAttribute("navTabId", navTabId);
    return "mm/supplier/supplier_view";
  }

  @RequestMapping("isExistsSupplier")
  @ResponseBody
  public Boolean isExistsSupplier(Model model, String supplierCode) {
    if (StringUtils.isBlank(supplierCode)) {
      return false;
    }
    Supplier supplier = supplierService.getSupplierBySupplierCode(this.getSiteCode(), supplierCode);
    if (supplier != null && !StringUtils.isBlank(supplier.getSupplierCode())) {
      return false;
    }
    return true;
  }

  @RequestMapping("saveSupplier")
  @ResponseBody
  public DWZResult saveSupplier(Supplier supplier, Model model, String type, String navTabId) {
    if (supplier == null || StringUtils.isBlank(supplier.getSupplierCode())) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "parameterNull.M"));
    }
    supplier.setSiteCode(this.getSiteCode());
    this.setCreateInfo(supplier);
    if ("add".equals(type)) {
      supplierService.saveSupplier(supplier);
    } else if ("modify".equals(type)) {
      supplierService.modifySupplier(supplier);
    }
    return dialogAjaxDone(navTabId, null, null, null);
  }

  @RequestMapping("deleteSupplier")
  @ResponseBody
  public DWZResult deleteSupplier(String ids, String navTabId, Model model, HttpServletRequest request) {
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }
    String[] idArr = ids.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>();
    int failures = 0;
    for (int i = 0; i < idArr.length; i++) {
      String message = supplierService.deleteSupplierBySiteCodeAndSupplierCode(this.getSiteCode(), idArr[i]);
      if (!StringUtils.isBlank(message)) {
        failures += 1;
        BatchJobReport report = new BatchJobReport(idArr[i], this.getImeViewMessageI18n(this.getCurrentLanguage(),
            message), false, this.getCurrentLanguage(), this.getSiteCode());
        reports.add(report);
      } else {
        BatchJobReport report = new BatchJobReport(idArr[i], "", true, this.getCurrentLanguage(), this.getSiteCode());
        reports.add(report);
      }
    }
    if (failures == 0) {
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(this.getCurrentLanguage(), "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, reports);
    }
  }

  @RequestMapping("supplierLookUp")
  public String supplierLookUp(Model model, @ModelAttribute("condition") SupplierCondition condition) {
    condition.setSiteCode(this.getSiteCode());
    condition.setLockFlag(0);
    List<Supplier> suppliers = supplierService.getSuppliersByCondition(condition);
    int count = supplierService.getSupplierCountByCondition(condition);
    condition.setData(suppliers);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    return "mm/supplier/supplier_select";
  }

  @RequestMapping("supplierSuggest")
  @ResponseBody
  public Object componentSuggest(@ModelAttribute("condition") SupplierCondition condition) {
    condition.setSiteCode(this.getSiteCode());
    condition.setLockFlag(0);
    return supplierService.getSuppliersSuggests(condition);
  }

  @RequestMapping("validateSupplier")
  @ResponseBody
  public boolean validateSupplier(Model model, String navTabId, String supplierCode) {
    Supplier supplier = supplierService.getSupplierBySupplierCode(getSiteCode(), supplierCode);
    return supplier != null;
  }
}
